package com.yuyou.train.lesson.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.lesson.service.IResourceQuestionService;
import com.yuyou.train.lesson.vo.req.ReqResQuesAdd;
import com.yuyou.train.lesson.vo.req.ReqResQuesUpdate;
import com.yuyou.train.lesson.vo.resp.RespResQuesDetail;
import com.yuyou.train.service.api.lesson.dto.QuestionPageDTO;
import com.yuyou.train.service.api.lesson.entity.ResourceQuestion;
import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.PageParams;
import com.yuyou.train.util.bean.Pagination;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.tool.DefindBeanUtils;

@Validated
@RestController
@RequestMapping(value="/api/question")
public class QuestionController extends BaseController{

	
	@Autowired
	private IResourceQuestionService resQuesService;
	
	
	@ApiOperation(value="新增题目")
	@RequestMapping(value="/addQues",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> addQues(@NotNull(message="JSON对象不能为空") @RequestBody(required=true) @ApiParam(name="JSON对象",value="传入JSON格式",required=true)
												   @Validated @ModelAttribute ReqResQuesAdd reqResQuesAdd,
												   HttpServletRequest request){

		LoginRedisObj loginObj = this.getUserObj(request);
		ResBoolSimpleInfo res = resQuesService.addQuestion(reqResQuesAdd, loginObj.getUserId());
		return ReturnResult.success(res);
	}


	@ApiOperation(value="编辑题目")
	@RequestMapping(value="/updateQues",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo<ResourceQuestion>> updateQues(@Validated @ModelAttribute ReqResQuesUpdate reqResQuesUpdate,
																		HttpServletRequest request){
		LoginRedisObj loginObj = this.getUserObj(request);
		ResBoolSimpleInfo<ResourceQuestion> res = resQuesService.updateQuestion(reqResQuesUpdate, loginObj.getUserId());
		return ReturnResult.success(res);
	}


	@ApiOperation(value="删除题目")
	@ApiImplicitParam(name = "id", value = "题目ID", paramType="query", required = true)
	@RequestMapping(value="/delQues",method=RequestMethod.POST)
	public ReturnResult<ResBoolSimpleInfo> delQues(@NotBlank (message="id不能为空") String id){
		ResBoolSimpleInfo res = resQuesService.delQuestion(id);
		return ReturnResult.success(res);
	}

	@ApiOperation(value = "题目详情")
	@ApiImplicitParam(name = "id", value = "题目ID", paramType="query", required = true)
	@RequestMapping(value="/detailQues",method=RequestMethod.POST)
	public ReturnResult<RespResQuesDetail> detailQues(@NotBlank (message="id不能为空")String id){
		ResourceQuestion resourceQuestion = resQuesService.selectByPrimaryKey(id);
		RespResQuesDetail resp = new RespResQuesDetail();
		DefindBeanUtils.copyProperties(resourceQuestion, resp);	
		return ReturnResult.success(resp);
	}
	
	@ApiOperation(value="题目分页")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "questionTag", value = "标签类型", paramType="query", required = false),
		@ApiImplicitParam(name = "isUse", value = "是否启用：0：否，1：是", paramType="query", required = false),
		@ApiImplicitParam(name = "questionType", value = "题型 01:选择题，02:填空题", paramType="query",required = false)}
		)
	@RequestMapping(value="/findQuesPage", method=RequestMethod.POST)
	public ReturnResult<Pagination<QuestionPageDTO>> findQuesPage(@Validated @ModelAttribute PageParams pageParams,
																  String questionTag,
																  String isUse,
																  String questionType,
																  String key){
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("dataQuestionTag", questionTag);
		conditions.put("isUse", isUse);
		conditions.put("questionType", questionType);
		conditions.put("key", key);
		Pagination<QuestionPageDTO> queryPgn = new Pagination<QuestionPageDTO>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total = resQuesService.findQuestionPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<QuestionPageDTO> list = resQuesService.findQuestionPageList(queryPgn);
			queryPgn.setList(list);
		}	
		return ReturnResult.success(queryPgn);
	}
}
